package com.wbr.file.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class EncryptUtil {
	
	private EncryptUtil(){}
	static
	private EncryptUtil instance=null;
	public EncryptUtil getInstance(){
		if(instance==null){
			instance=new EncryptUtil();
		}
		return instance;
	}
	
	
	private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";//"算法/模式/补码方式"

	/**
	 * 传入文本内容，返回 SHA-256 串
	 * 
	 * @param strText
	 * @return
	 */
	public static String SHA256(final String strText) {
		return SHA(strText, "SHA-256");
	}

	/**
	 * 传入文本内容，返回 SHA-512 串
	 * 
	 * @param strText
	 * @return
	 */
	public static String SHA512(final String strText) {
		return SHA(strText, "SHA-512");
	}

	/**
	 * 字符串 SHA 加密
	 * 
	 * @param strSourceText
	 * @return
	 */
	private static String SHA(final String strText, final String strType) {
		// 返回值
		String strResult = null;

		// 是否是有效字符串
		if (strText != null && strText.length() > 0) {
			try {
				// SHA 加密开始
				// 创建加密对象 并传入加密类型
				MessageDigest messageDigest = MessageDigest.getInstance(strType);
				// 传入要加密的字符串
				messageDigest.update(strText.getBytes());
				// 得到 byte 类型结果
				byte byteBuffer[] = messageDigest.digest();

				// 将byte 转换为 string
				StringBuffer strHexString = new StringBuffer();
				// 遍历byte buffer
				for (int i = 0; i < byteBuffer.length; i++) {
					String hex = Integer.toHexString(0xff & byteBuffer[i]);
					if (hex.length() == 1) {
						strHexString.append('0');
					}
					strHexString.append(hex);
				}
				// 得到返回結果
				strResult = strHexString.toString();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
		}

		return strResult;
	}

	/**
	 * base64加密方法
	 * 
	 * @param plainText
	 * @return
	 */
	public static String getEncodedBase64(String plainText) {
		String encoded = null;
		try {
			byte[] bytes = plainText.getBytes("UTF-8");
			encoded = Base64.getEncoder().encodeToString(bytes);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encoded;
	}
	private static String getEncodedBase64(byte[] bytes){
		return Base64.getEncoder().encodeToString(bytes);
	}

	/**
	 * BASE64解密算法
	 * 
	 * @param plainText
	 * @return
	 */
	public static byte[] getDecodedBase64(String plainText) {
		byte[] decoded = null;
		try {
			byte[] bytes = plainText.getBytes("UTF-8");
			decoded = Base64.getDecoder().decode(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decoded;
	}
	/**
	 * AES加密 
	 * @param content 明文
	 * @param encryptKey 密码
	 * @return String AES加密内容
	 * @throws Exception
	 */
	private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(ALGORITHMSTR);//创建密码器
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES")); //初始化

		return cipher.doFinal(content.getBytes("utf-8"));
	}
	/**
	 * 对明文进行AES加密再进行BASE64加密
	 * @param content AES加密内容
	 * @param encryptKey 密码
	 * @return String AES->BASE64加密内容
	 * @throws Exception
	 */
	public static String aesEncrypt(String content, String encryptKey) throws Exception {
		return getEncodedBase64(aesEncryptToBytes(content, encryptKey));
	}
	/**
	 * AES解密
	 * @param encryptBytes AES加密的内容
	 * @param decryptKey 密码
	 * @return String 明文
	 * @throws Exception
	 */
	private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		return new String(decryptBytes);
	}
	/**
	 * BASE64解密
	 * @param encryptStr 经AES->BASE64加密的内容
	 * @param decryptKey 密码
	 * @return String AES解密后的BASE64加密的内容
	 * @throws Exception
	 */
	public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
		return aesDecryptByBytes(getDecodedBase64(encryptStr), decryptKey);
	}

}
